package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSONObject;
import com.hp.bon.logreverse.ExtractHelper;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery.utils.HttpClientUtils;
import com.hp.bon.sgw.wh.service.UserResourceConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class NormalPaySlot implements BizSlot {

    private static final Logger _log = LoggerFactory.getLogger(RechargeQueryBizSlot.class);


    public static void jsonToXmlStr(BizParam bizParam, StringBuffer buffer, String accNbr, List<String> list, String startTime, String endTime) {
        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<HqCheck>");
        buffer.append("<BillingMode>1</ BillingMode >");
        buffer.append("<MDN>" + accNbr + "</MDN>");
        buffer.append("<ServId>" + bizParam.getParamString("prodInstId") + "</ServId>");
        buffer.append("<AreaCode>" + bizParam.getParamString("regionId") + "</AreaCode>");
        buffer.append("<CkMDNlist>");
        for (int i = 0; i < list.size(); i++) {
            buffer.append("<CkMDN>" + list.get(i) + "</CkMDN>");
        }
        buffer.append("</CkMDNlist>");
        buffer.append("<StartTime>" + startTime + "</StartTime>");
        buffer.append("<EndTime>" + endTime + "</EndTime>");
        buffer.append("</HqCheck>");
    }

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {
            String paymentSerialNbr = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/Write_Off/Payment_Serial_Nbr");
            String bankCodeSet =UserResourceConfigService.getValue("Bank_code_set");
            String destinationId = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/Write_Off/Acc_Nbr");
            String bankCode = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/Bank_code");
            String feeAmount = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/Write_Off/Payment_Amount");
            String PaymentMethod = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/Write_Off/Payment_Method");


            String chargeSource = "";
            String[] bankCodeSets = bankCodeSet.split(",");
            for (String item : bankCodeSets) {
                if (String.valueOf(item.split("\\|")[0]).equals(bankCode)) {
                    chargeSource = String.valueOf(item.split("\\|")[1]);
                }
            }


            JSONObject recharge = new JSONObject();
            recharge.put("reqSerial", paymentSerialNbr);
            recharge.put("chargeSource", chargeSource);
            recharge.put("destinationId", destinationId);
            recharge.put("destinationIdType", "2");
            recharge.put("destinationAttr", "99");
            recharge.put("balanceItemTypeID", "0");
            recharge.put("unitTypeID", "0");
            recharge.put("feeAmount", feeAmount);
            recharge.put("BankCode", bankCode);
            recharge.put("PaymentMethod", PaymentMethod);
            JSONObject stdCcrRecharge = new JSONObject();
            stdCcrRecharge.put("recharge", recharge);

            com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
            jsonObject.put("stdCcrRecharge", stdCcrRecharge);

            StringBuilder buildStringMes = new StringBuilder();
            buildStringMes.append(jsonObject.toJSONString());
            //新接口新域名
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
            _log.info("request body is {}", buildStringMes.toString());
            _log.info("webserviceUrl is {}", webserviceUrl);
            String resp = HttpClientUtils.postHttpStr(buildStringMes.toString(), webserviceUrl);
            _log.info("response is {}", resp);
            if (resp.equals("")) {
                resp = com.hp.bon.sgw.wh.channel.invoices.bizSlot.HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL, "", "", "");
            }
            bizResult.setHttpBody(resp);
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }
}
